/**
 * Created by 黄啸林 on 2017/6/3.
 */
angular.module('admin.module', [])
    .controller('adminHomeCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,ionicDatePicker,$filter,$ionicPopup) {
    $scope.date = strDate(new Date());
    function strDate(date) {
        return $filter('date')(date, "yyyy-MM-dd");
    }
    $scope.getOrder = function (date) {
        $http.get(API('huanbi/findBankOrder?date='+date)).success(function (result) {
           if(result.successful){
                $scope.orders = result.data;
               $scope.orders.sort(function(a,b){
                   return b.orderDate-a.orderDate});
           }else{
                $scope.$error=false;
           }
        });
    }
    $scope.getOrder($scope.date);

    $scope.confirmOrder = function (index) {
        var orderId = $scope.orders[index].orderId;
        $http.get(API('huanbi/confirmOrder?orderId='+orderId)).success(function (result) {
            if(result.successful){
                var alertPopup = $ionicPopup.alert({
                    title: '温馨提示!',
                    template: '提交成功！'
                });
                alertPopup.then(function(res) {
                    $scope.orders[index].orderStatus=1;
                });
            }
        })
    }

    $scope.toPage = function (day) {
        var newDate = new Date($scope.date);
        if(day=='nextDay'){
            $scope.date = strDate(newDate.setDate(newDate.getDate()+1));
            $scope.getOrder($scope.date);
        }else if(day=='beforeDay'){
            $scope.date = strDate(newDate.setDate(newDate.getDate()-1));
            $scope.getOrder($scope.date);
        }
    }
})
    .controller('adminWorkCtrl', function ($scope,$rootScope, $state, $http,$ionicLoading,ionicDatePicker) {
        $scope.user={};
        $scope.submit = function () {
            var orderBy = "phone="+$scope.user.phone
                +"&cardId="+$scope.user.cardId
                +"&userName="+$scope.user.userName
                +"&openId="+$rootScope.User.openId;

            $http.get(API("huanbi/bindUser?")+orderBy).success(function (data) {
                console.log(data);
                $state.go("tabs.home");
            })
        }
    })
    .controller('adminDetailsCtrl',function ($scope,$rootScope, $state, $http,$ionicPopup) {
        $scope.data={};
        $scope.update=function () {
            var data ={
                'account':$scope.data.account,
                'oldPassword':md5($scope.data.oldPassword),
                'newPasword':md5($scope.data.newPasword),
                'confirmPassword':md5($scope.data.confirmPassword)
            }
            $http.post(API('huanbi/staff/changePassword'),data).success(function (result) {
                if(result.successful){
                    var alertPopup = $ionicPopup.alert({
                        title: '兑换提示!',
                        template: '密码修改成功！'
                    });
                    alertPopup.then(function(res) {
                        $state.go('login');
                    });
                }else {
                    var alertPopup = $ionicPopup.alert({
                        title: '兑换提示!',
                        template: '密码修改失败！'
                    });
                    alertPopup.then(function(res) {
                    });
                }
            })
        }
    })